package com.example.poi;


import org.apache.poi.ss.usermodel.Workbook;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class ExcelUtils {

    public <T> Workbook createWorkbook(List<T> arr) throws IllegalAccessException {
        if (arr != null && arr.size() != 0) {
            List<String> head = new ArrayList<>();
            List<List<String>> dataList = new ArrayList<>();
            boolean initHeadValue = false;

            Field[] fields = arr.get(0).getClass().getDeclaredFields();

            /**
             * 排序Sort
             */
            List<Field> fieldList = Arrays.stream(fields)
                    .sorted((o1, o2) -> {
                        boolean b1 = o1.isAnnotationPresent(Excel.class);
                        boolean b2 = o2.isAnnotationPresent(Excel.class);

                        if (b1 && b2) {
                            Excel o1Annotation = o1.getAnnotation(Excel.class);
                            Excel o2Annotation = o2.getAnnotation(Excel.class);

                            return o1Annotation.sort() - o2Annotation.sort();
                        }

                        return b1 ? -1 : 1;
                    }).collect(Collectors.toList());

            for (T t : arr) {
                List<String> rowData = new ArrayList<>();

                for (Field field : fieldList) {
                    if (field.isAnnotationPresent(Excel.class)) {
                        field.setAccessible(true);
                        Excel excel = field.getAnnotation(Excel.class);

                        String colName = "".equals(excel.value()) ? field.getName() : excel.value();
                        Object value = field.get(t);

                        if (!initHeadValue) {
                            head.add(colName);
                        }

                        rowData.add(value + "");
                    }
                }

                initHeadValue = true;
                dataList.add(rowData);
            }

            Workbook wb = POIUtils.createWorkBook("2007", null, head, dataList);

            return wb;
        }

        throw new IllegalArgumentException("数组列表不能为空");
    }


    public static <T> List<T> readWorkbook(Class<T> clazz, InputStream is, String sheetName) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        List<T> arr = new ArrayList<>();

        List<Map<String, String>> list = POIUtils.readExcel(is, sheetName);


        if (list != null && list.size() != 0) {

            for (int i = 0; i < list.size(); i++) {
                Map<String, String> row = list.get(i);

                T t = clazz.getDeclaredConstructor().newInstance();

                Field[] fields = t.getClass().getFields();

                //通过遍历对象字段来填充数据
                for (Field field : fields) {
                    field.setAccessible(true);
                    Excel excel = field.getAnnotation(Excel.class);

                    if (excel != null) {
                        String colName = "".equals(excel.value()) ? field.getName() : excel.value();

                        if (row.containsKey(colName)) {
                            String value = row.get(colName);
                            field.set(t, value);
                        }
                    }
                }

                arr.add(t);
            }
        }

        return arr;
    }
}
